﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Configuration;
using WMPLib;
using System.Net;

namespace WJDHApp
{
    public partial class Form1 : Form
    {
        System.Timers.Timer t;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //启功监控
                if (button1.Text == "启动监控")
                {
                    double m = Convert.ToDouble(textBox3.Text) * 1000;
                    if (t == null)
                    {
                        t = new System.Timers.Timer(m);
                    }
                    button1.Text = "暂停监控";
                    t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
                    t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
                else
                {
                    t.Enabled = false;
                    button1.Text = "启动监控";
                }
            }
            catch (Exception ex)
            {
                textBox2.Text = ex.Message;
            }
        }

        int idx = 0;
        int a = 0;
        //bool showdg = false;
        public void theout(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                a++;
                if (a % 10 == 0)
                {
                    textBox2.Text = "";//清空
                }
                var values = GetJson();

                if (Convert.ToInt32(values) > 0)
                {
                    idx++;
                    Invoke((MethodInvoker)delegate
                    {
                        textBox2.Text = textBox2.Text.Insert(0, "可以购买了" + Environment.NewLine);
                        PlayThread();
                        //textBox2.Text += "可以购买了" + "\r\n";
                    });
                    if (idx <= 2)
                    {
                        MessageBox.Show("提示", "可以购买了", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                    }
                }
                else
                {
                    Invoke((MethodInvoker)delegate
                    {
                        textBox2.Text = values + "  " + textBox2.Text.Insert(0, "不可购买" + a.ToString() + Environment.NewLine);
                        //textBox2.Text += "不可购买" + "\r\n";
                    });
                }
            }
            catch (Exception ex)
            {
                Invoke((MethodInvoker)delegate
                {
                    textBox2.Text = textBox2.Text.Insert(0, ex.Message + Environment.NewLine);
                });
                //MessageBox.Show("提示", "出错了！！！！！", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
        }

        //测试按钮
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string jsonStr = GetJson();
                //string json = HttpHelper.ConvertJsonString(jsonStr);
                textBox2.Text = jsonStr;
            }
            catch (Exception ex)
            {
                textBox2.Text = ex.Message;
            }

        }

        private string GetJson()
        {
            string cook = textBox1.Text;
            //string posturl = "api.wjdh.jwanjia.net/yiwucard/detail";
            //var json = HttpHelper.HttpPost(posturl, postparams);
            string geturl = "https://wanjia.9yiwu.com/subscription/available";

            CookieContainer cc = new CookieContainer();
            cc.Add(new System.Uri("https://wanjia.9yiwu.com"), new Cookie("WJSESSIONID", cook));

            var json = HttpHelper.SendDataByGET(geturl, "", cc);
            return json;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            textBox3.Text = "5";
            textBox1.Text = ConfigurationManager.AppSettings["postParams"];
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (axWindowsMediaPlayer1.playState != WMPPlayState.wmppsPlaying)
            {
                PlayThread();
            }
            else
            {
                axWindowsMediaPlayer1.Ctlcontrols.stop();
            }

        }

        private void PlayThread()
        {
            axWindowsMediaPlayer1.URL = System.Environment.CurrentDirectory + @"\Levitate.mp3";//I:\CloudMusic\胡伟立 - 随缘(粤).mp3
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            productForm formp = new productForm();
            formp.Show();
        }
    }
}
